package com.xcjh.controller;

import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xcjh.domain.DataAcquisition;
import com.xcjh.eums.AppType;
import com.xcjh.service.DataAcquisitionService;
import com.xcjh.service.impl.MarrayuServiceImpl;
import com.xcjh.service.impl.YiBanServiceImpl;
import com.xcjh.service.impl.YiduiServiceImpl;
import com.xcjh.service.impl.ZhenaiServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/fiddler")
public class SaveLogController {

    @Autowired
    private YiduiServiceImpl yiduiServiceImpl;
    @Autowired
    private YiBanServiceImpl yiBanServiceImpl;
    @Autowired
    private ZhenaiServiceImpl zhenaiServiceImpl;
    @Autowired
    private MarrayuServiceImpl marrayuServiceImpl;
    @Autowired
    private DataAcquisitionService dataAcquisitionService;

    /**
     * Marray U
     */
    @PostMapping("/user/detail_view")
    public String marraySaveLog(@RequestBody String body){
        marrayuServiceImpl.save(body);
        return "ok";
    }

    /**
     * Marray U
     * @param body
     * @return
     */
    @PostMapping("/user_info")
    public String userInfo(@RequestBody String body){
        marrayuServiceImpl.save(body);
        return "ok";
    }


    /**
     * 伊对
     */
    @PostMapping("/v3/members/info")
    public String yiduiSaveLog(@RequestBody String body){
        yiduiServiceImpl.save(body);
        return "ok";
    }

    /**
     * 一伴
     */
    @PostMapping("/yiban-api/resume/get")
    public String yiBanSaveLog(@RequestBody String body){
        yiBanServiceImpl.save(body);
        return "ok";
    }


    /**
     * 珍爱分页保存数据
     */
    @PostMapping("/api/search/getConditionData.do")
    public List<String> zhenSaveLog(@RequestBody String body){
        return zhenaiServiceImpl.insertData(body);
    }
    /**
     * 珍爱 发消息页面推送消息
     */
    @PostMapping("/n/message/_detail")
    public String sendMessages(@RequestBody String body){
        String http=body.substring(0,body.indexOf("\r")).split(" ")[1];
        URL url = URLUtil.url(http);
        String appId=(url.getQuery().split("=")[1]);
        zhenaiServiceImpl.sendApp(appId);
        return "";
    }
    @PostMapping("/zhenai/check")
    public List<String> sendMessages(@RequestBody List<String> body, HttpServletResponse arg1){
        arg1.setHeader("Access-Control-Allow-Origin", "*");
        arg1.setHeader("Access-Control-Allow-Method", "POST,GET");

        return zhenaiServiceImpl.check(body);
    }


    /**
     * 发送消息
     * @param appId
     * @return
     */
    @GetMapping("/zhenai/send")
    public String zhenSend(String appId){
        return zhenaiServiceImpl.sendApp(appId);
    }


    /**
     * 佳缘 发消息
     * @param response
     * @param in
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/gift_commend.php")
    public String put(HttpServletResponse response,@RequestBody String in) throws Exception {
        String http=in.substring(0,in.indexOf("\r")).split(" ")[1];
        URL url = URLUtil.url(http);
        String appId=(url.getQuery().split("=")[1]);
        appId=appId.split("&")[0];
        System.out.println(appId);

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Method", "POST,GET");

        zhenaiServiceImpl.sendJiayuanApp(appId);
        return "";
    }

}
